
module.exports = {
	
	/**
	 * 更新订单状态表
	 * @param {object} data 要更新的数据
	 * @param {string} orderNo 要更新的记录的订单号
	 * @returns {object} 更新结果
	 */
	
	updateOrderStatusByOrderNo(orderNo,data) {
		try{
			const db = uniCloud.database();
			return db.collection('order_status').where({order_no:orderNo}).update({
				order_status:db.command.push(data)
			})
		}catch(e){
			return e;
		}
	},
	/**
	 * 转单后将骑手表的订单数量减1
*/
	updateRiderOrderNumberInc(riderId,num=-1){
		const db = uniCloud.database();
		return db.collection('rider_users').where({_id:riderId}).update({
			order_number:db.command.inc(num),
			order_total_number:db.command.inc(num)
		});
	},
	/**
	 * 修改骑手金额和订单数量
*/
	setRiderEarningsAndOrderNumbers(riderId,money=0,orderNum=0){
		const db = uniCloud.database();
		return db.collection('rider_users').where({_id:riderId}).update({
			money:db.command.inc(money),
			order_number:db.command.inc(orderNum)
		});
	},
	//更新骑手的月销量和总销量
	updateRiderUsersById(riderId,totalNumber=1,price){
		const db = uniCloud.database();
		var cmd = db.command;
		var data = {
			order_total_number:cmd.inc(totalNumber),
			money:cmd.inc(price)
			
		}
		return db.collection('rider_users').where({_id:riderId}).update(data);
	},
	
	/**
		 *作用：删除订单表中的骑手信息
		 * @param orderNo:订单号
		 */
	async removeRiderByOrderNo(orderNo){
			const db = uniCloud.database();
			var cmd = db.command;
			return await db.collection('orders').where({order_no:orderNo}).update({
				rider:cmd.remove()
			});
		},
		
	/**
	 *作用：根据订单号删除骑手账单记录
	 * @param {Object} orderNo:订单号
	 */
	async removeRiderBillByOrderNo(orderNo){
		const db = uniCloud.database();
		var cmd = db.command;
		return await db.collection('rider_bill').where({
			'order.order_no':orderNo
		}).remove();
	},
	/**
	 * 作用：插入骑手账单表
	 */
	async addRiderToRiderRill(order,rider){
		const db = uniCloud.database();
		var cmd = db.command;
		//插入骑手账单表
		var date = new Date();
		var timestampDate = date.getTime();
		var today = (date.getMonth()+1)+'-'+date.getDay();
		var month = date.getMonth()+1;
		var riderBillData = {
			 "price": order.deduct_delivery_price,
			    "rider_id": rider._id,
			    "user_id": order.user_id,
			    "shop_id": order.shop_id,
			    "order": {
			        "order_no": order.order_no,
			        "total_fee": order.total_price,
			        "delivery_fee": order.delivery_price
			    },
			    "type": 1,
			    "text": "配送佣金",
			    "status": 1,
			    "create_date": {
			        "timestamp": timestampDate,
			        "date": today,
			        "month": month
			    }
		}
		//插入骑手佣金表
		return await db.collection('rider_bill').add(riderBillData);
	}
}
